package com.chengyu.core.service.walk.impl;

import cn.hutool.core.date.DateUtil;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.*;
import com.chengyu.core.model.*;
import com.chengyu.core.service.walk.WalkTrendsDownloadService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WalkTrendsDownloadServiceImpl implements WalkTrendsDownloadService {

    @Autowired
    private WalkTrendsDownloadMapper walkTrendsDownloadMapper;
    @Autowired
    private PmsGoodsMapper pmsGoodsMapper;
    @Autowired
    private UmsShopMapper umsShopMapper;
    @Autowired
    private WalkTrendsDownloadNumMapper walkTrendsDownloadNumMapper;
    @Autowired
    private WalkMemberLinkMemberMapper walkMemberMapper;
    @Autowired
    private WalkTrendsGoodsMapper walkTrendsGoodsMapper;

    @Autowired
    private WalkTrendsMapper walkTrendsMapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addDownloadLog(WalkTrendsDownload walkTrendsDownload,WalkMember walkMember) {
        //根据动态id查看他的对应的商品

        PmsGoods pmsGoods = pmsGoodsMapper.selectByPrimaryKey(walkTrendsDownload.getPublishGoodsId());


        walkTrendsDownload.setPublishGoodsId(pmsGoods.getId());
        walkTrendsDownload.setPublishGoodsName(pmsGoods.getTitle());
        walkTrendsDownload.setPublishGoodsHeadImg(pmsGoods.getMainImg());
        walkTrendsDownload.setPublishGoodsPrice(pmsGoods.getPrice());
        walkTrendsDownload.setPublishGoodsTrailerLink(pmsGoods.getTrailerLink());

        walkTrendsDownload.setAddBy(walkMember.getNickname());
        walkTrendsDownload.setDownloadWalkMemberId(walkMember.getId());
        walkTrendsDownload.setDownloadWalkMemberName(walkMember.getNickname());
        walkTrendsDownload.setDownloadWalkMemberHeadImg(walkMember.getHeadImg());
        walkTrendsDownload.setStatus(1);

        walkTrendsDownload.setAddTime(DateUtil.date());

        UmsShop umsShop = umsShopMapper.selectByPrimaryKey(pmsGoods.getShopId());
        walkTrendsDownload.setPublishShopId(umsShop.getId());
        walkTrendsDownload.setPublishShopName(umsShop.getName());
        walkTrendsDownload.setPublishShopHeadImg(umsShop.getLogo());
        walkTrendsDownloadMapper.insertSelective(walkTrendsDownload);

        //添加下载记录的同时去选中的申请记录扣减相应的数量
        WalkTrendsDownloadNum walkTrendsDownloadNumSelect = walkTrendsDownloadNumMapper.selectByPrimaryKey(walkTrendsDownload.getDownloadNumId());

        WalkTrendsDownloadNum walkTrendsDownloadNum = new WalkTrendsDownloadNum();
        walkTrendsDownloadNum.setId(walkTrendsDownload.getDownloadNumId());
        walkTrendsDownloadNum.setUseNum(walkTrendsDownloadNumSelect.getUseNum()-1);
        walkTrendsDownloadNumMapper.updateByPrimaryKeySelective(walkTrendsDownloadNum);

        //同时修改此商品的素材下载量字段
        PmsGoods pmsGoodsUpdate = new PmsGoods();
        pmsGoodsUpdate.setId(pmsGoods.getId());
        if (walkTrendsDownload.getType()==1){
            pmsGoodsUpdate.setTrendsDownloadNumVideo(pmsGoods.getTrendsDownloadNumVideo()+1);
        }else if (walkTrendsDownload.getType()==3){
            pmsGoodsUpdate.setTrendsDownloadNumImg(pmsGoods.getTrendsDownloadNumImg()+1);
        }
        //同时修改这个素材的所属
        WalkTrends walkTrendsUpdate = new WalkTrends();
        walkTrendsUpdate.setId(walkTrendsDownload.getTrendsId());
        walkTrendsUpdate.setBelongMemberId(walkMember.getMemberId());
        walkTrendsMapper.updateByPrimaryKeySelective(walkTrendsUpdate);

        pmsGoodsMapper.updateByPrimaryKeySelective(pmsGoodsUpdate);
    }

    @Override
    public CommonPage<WalkTrendsDownload> listDownloadLog(Date dateFrom,Date dateTo,Integer type, Integer shopId,Integer walkMemberId, Integer page, Integer pageSize) throws ServiceException {
        PageHelper.startPage(page, pageSize);
        WalkTrendsDownloadExample example = new WalkTrendsDownloadExample();
        example.setOrderByClause("add_time desc");
        WalkTrendsDownloadExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(CommonConstant.YES_INT);
        if (walkMemberId!=null){
            criteria.andDownloadWalkMemberIdEqualTo(walkMemberId);
        }
       if (shopId!=null){
           criteria.andPublishShopIdEqualTo(shopId);
       }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if(dateFrom != null && dateTo != null){
            criteria.andAddTimeBetween(dateFrom, dateTo);
        }

        List<WalkTrendsDownload> walkTrendsDownloadList = walkTrendsDownloadMapper.selectByExampleWithBLOBs(example);
        return CommonPage.restPage(walkTrendsDownloadList);
    }

    @Override
    public CommonResult<WalkTrendsDownloadNum> verifyTrendsDownload(Integer trendsId,Integer id,Integer goodsId) throws ServiceException {
        //判断id是否存在
        WalkTrendsDownloadNum walkTrendsDownloadNum = walkTrendsDownloadNumMapper.selectByPrimaryKey(id);


        WalkTrends walkTrends = walkTrendsMapper.selectByPrimaryKey(trendsId);

        if (!walkTrendsDownloadNum.getType().equals(walkTrends.getType())){
            throw new ServiceException("下载的素材类型和选择的申请类型不一致");
        }

        if (walkTrendsDownloadNum==null){
            throw new ServiceException("申请记录不存在");
        }
        if (walkTrendsDownloadNum.getUseNum()==0){
            throw new ServiceException("此条申请的下载次数已经用完");
        }
        if (walkTrendsDownloadNum.getGoodsId()!=null&& !walkTrendsDownloadNum.getGoodsId().equals(goodsId)){
            throw new ServiceException("下载素材归属的商品与申请的商品不一样");
        }

        return CommonResult.success(null);
    }

    @Override
    public CommonResult<WalkTrendsDownloadNum> verifyTrendsDownloadIsOther(Integer trendsId) throws ServiceException {
        WalkTrendsExample walkTrendsExample = new WalkTrendsExample();
        walkTrendsExample.createCriteria().andIdEqualTo(trendsId).andBelongMemberIdNotEqualTo(0);

        List<WalkTrends> walkTrends = walkTrendsMapper.selectByExample(walkTrendsExample);
        if (walkTrends.size()>0){
            throw new ServiceException("此素材已被下载，请选择其他素材或重新获取");
        }

        return CommonResult.success(null);
    }

    @Override
    public CommonResult<WalkTrendsDownload> getDownLoadInfo(Integer walkMemberId, Integer trendsId) throws ServiceException {
        WalkTrendsDownloadExample example = new WalkTrendsDownloadExample();
        example.createCriteria().andDownloadWalkMemberIdEqualTo(walkMemberId).andTrendsIdEqualTo(trendsId);
        List<WalkTrendsDownload> walkTrendsDownloads = walkTrendsDownloadMapper.selectByExample(example);
        if (walkTrendsDownloads.size()==0){
            throw new ServiceException("下载记录不存在");
        }
        return CommonResult.success(walkTrendsDownloads.get(0));
    }


}
